package com.ifacebox.client.tools;

import com.ifacebox.client.tools.utils.PathUtils;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.Properties;

/**
 * @author znn
 */
public class FaceLuckDrawConfig {
    public static final String FILE_PATH = PathUtils.getLocalPath();
    public static final String FILE_NAME = "/faceLuckDrawConfig.properties";
    public static final File CONFIG_LUCK_DRAW_FILE = new File(FILE_PATH, FILE_NAME);
    private final Properties configProperties;
    private int screenId = 0;
    private Rectangle screenRectangle;
    private int faceImageCols = 4 * 4;
    private int faceImageRows = 2 * 4;
    private int faceImageGap = 1;
    private int faceImageSwitchMs = 100;
    private int faceImageTotal;
    private Dimension faceImageSize;
    private ImageIcon faceImageIcon;
    private int faceLuckDrawCountdown = 3;
    private boolean faceImageLuckDrawSwitch = true;
    private boolean faceLuckWinRemove = true;
    private String faceLuckDrawTips;
    private String faceImageFilePath = "/faceLuckDraw/faceImageFile";
    private String facePersonFilePath = "/faceLuckDraw/facePersonInfo";
    private String faceLuckDrawListFile = "/faceLuckDraw/faceLuckDrawList.txt";
    private File[] faceImageFile;
    private File facePersonPath, faceLuckDrawList;

    public FaceLuckDrawConfig() {
        this.configProperties = new Properties();
        this.init();
        GraphicsEnvironment screenEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice screenDevice = screenEnvironment.getDefaultScreenDevice();
        GraphicsDevice[] screenDevices = screenEnvironment.getScreenDevices();
        for (int i = 0; i < screenDevices.length; i++) {
            if (this.screenId == i) {
                screenDevice = screenDevices[i];
            }
        }
        this.setScreenRectangle(screenDevice.getDefaultConfiguration().getBounds());

        this.setFaceImageTotal(this.faceImageRows * this.faceImageCols);
        this.setFaceImageSize(new Dimension());
        this.faceImageSize.width = this.screenRectangle.width / this.faceImageCols - this.faceImageGap;
        this.faceImageSize.height = this.screenRectangle.height / this.faceImageRows - this.faceImageGap;
        this.setFaceImageIcon(new ImageIcon(this.getClass().getResource("/static/face-image.jpg")));
        this.faceImageIcon.setImage(this.faceImageIcon.getImage().getScaledInstance(this.faceImageSize.width, this.faceImageSize.height, Image.SCALE_SMOOTH));

        this.setFaceImageFile(new File(FILE_PATH, this.faceImageFilePath).listFiles());
        this.setFacePersonPath(new File(FILE_PATH, this.facePersonFilePath));
        this.setFaceLuckDrawList(new File(FILE_PATH, this.faceLuckDrawListFile));
    }

    public void init() {
        try (InputStream is = CONFIG_LUCK_DRAW_FILE.exists() ? new FileInputStream(CONFIG_LUCK_DRAW_FILE) : FaceLuckDrawConfig.class.getResourceAsStream(FILE_NAME)) {
            configProperties.load(is);
            screenId = Integer.parseInt(configProperties.getProperty("screenId", String.valueOf(screenId)));
            faceImageCols = Integer.parseInt(configProperties.getProperty("faceImageCols", String.valueOf(faceImageCols)));
            faceImageRows = Integer.parseInt(configProperties.getProperty("faceImageRows", String.valueOf(faceImageRows)));
            faceImageGap = Integer.parseInt(configProperties.getProperty("faceImageGap", String.valueOf(faceImageGap)));
            faceImageSwitchMs = Integer.parseInt(configProperties.getProperty("faceImageSwitchMs", String.valueOf(faceImageSwitchMs)));
            faceLuckDrawCountdown = Integer.parseInt(configProperties.getProperty("faceLuckDrawCountdown", String.valueOf(faceLuckDrawCountdown)));
            faceImageLuckDrawSwitch = Boolean.parseBoolean(configProperties.getProperty("faceImageLuckDrawSwitch", String.valueOf(faceImageLuckDrawSwitch)));
            faceLuckWinRemove = Boolean.parseBoolean(configProperties.getProperty("faceLuckWinRemove", String.valueOf(faceLuckWinRemove)));
            faceLuckDrawTips = configProperties.getProperty("faceLuckDrawTips", faceLuckDrawTips);
            faceImageFilePath = configProperties.getProperty("faceImageFilePath", faceImageFilePath);
            facePersonFilePath = configProperties.getProperty("facePersonFilePath", facePersonFilePath);
            faceLuckDrawListFile = configProperties.getProperty("faceLuckDrawListFile", faceLuckDrawListFile);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    public void save() {
        try (OutputStream os = new FileOutputStream(CONFIG_LUCK_DRAW_FILE)) {
            configProperties.setProperty("screenId", String.valueOf(screenId));
            configProperties.setProperty("faceImageCols", String.valueOf(faceImageCols));
            configProperties.setProperty("faceImageRows", String.valueOf(faceImageRows));
            configProperties.setProperty("faceImageGap", String.valueOf(faceImageGap));
            configProperties.setProperty("faceImageSwitchMs", String.valueOf(faceImageSwitchMs));
            configProperties.setProperty("faceLuckDrawCountdown", String.valueOf(faceLuckDrawCountdown));
            configProperties.setProperty("faceImageLuckDrawSwitch", String.valueOf(faceImageLuckDrawSwitch));
            configProperties.setProperty("faceLuckWinRemove", String.valueOf(faceLuckWinRemove));
            configProperties.setProperty("faceLuckDrawTips", faceLuckDrawTips);
            configProperties.setProperty("faceImageFilePath", faceImageFilePath);
            configProperties.setProperty("facePersonFilePath", facePersonFilePath);
            configProperties.setProperty("faceLuckDrawListFile", faceLuckDrawListFile);
            configProperties.store(os, FaceLuckDrawConfig.this.getClass().getSimpleName());
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    public int getScreenId() {
        return screenId;
    }

    public void setScreenId(int screenId) {
        this.screenId = screenId;
    }

    public Rectangle getScreenRectangle() {
        return screenRectangle;
    }

    public void setScreenRectangle(Rectangle screenRectangle) {
        this.screenRectangle = screenRectangle;
    }

    public int getFaceImageCols() {
        return faceImageCols;
    }

    public void setFaceImageCols(int faceImageCols) {
        this.faceImageCols = faceImageCols;
    }

    public int getFaceImageRows() {
        return faceImageRows;
    }

    public void setFaceImageRows(int faceImageRows) {
        this.faceImageRows = faceImageRows;
    }

    public int getFaceImageGap() {
        return faceImageGap;
    }

    public void setFaceImageGap(int faceImageGap) {
        this.faceImageGap = faceImageGap;
    }

    public int getFaceImageSwitchMs() {
        return faceImageSwitchMs;
    }

    public void setFaceImageSwitchMs(int faceImageSwitchMs) {
        this.faceImageSwitchMs = faceImageSwitchMs;
    }

    public int getFaceImageTotal() {
        return faceImageTotal;
    }

    public void setFaceImageTotal(int faceImageTotal) {
        this.faceImageTotal = faceImageTotal;
    }

    public Dimension getFaceImageSize() {
        return faceImageSize;
    }

    public void setFaceImageSize(Dimension faceImageSize) {
        this.faceImageSize = faceImageSize;
    }

    public ImageIcon getFaceImageIcon() {
        return faceImageIcon;
    }

    public void setFaceImageIcon(ImageIcon faceImageIcon) {
        this.faceImageIcon = faceImageIcon;
    }

    public int getFaceLuckDrawCountdown() {
        return faceLuckDrawCountdown;
    }

    public void setFaceLuckDrawCountdown(int faceLuckDrawCountdown) {
        this.faceLuckDrawCountdown = faceLuckDrawCountdown;
    }

    public boolean isFaceImageLuckDrawSwitch() {
        return faceImageLuckDrawSwitch;
    }

    public void setFaceImageLuckDrawSwitch(boolean faceImageLuckDrawSwitch) {
        this.faceImageLuckDrawSwitch = faceImageLuckDrawSwitch;
    }

    public boolean isFaceLuckWinRemove() {
        return faceLuckWinRemove;
    }

    public void setFaceLuckWinRemove(boolean faceLuckWinRemove) {
        this.faceLuckWinRemove = faceLuckWinRemove;
    }

    public String getFaceLuckDrawTips() {
        return faceLuckDrawTips;
    }

    public void setFaceLuckDrawTips(String faceLuckDrawTips) {
        this.faceLuckDrawTips = faceLuckDrawTips;
    }

    public String getFaceImageFilePath() {
        return faceImageFilePath;
    }

    public void setFaceImageFilePath(String faceImageFilePath) {
        this.faceImageFilePath = faceImageFilePath;
    }

    public String getFacePersonFilePath() {
        return facePersonFilePath;
    }

    public void setFacePersonFilePath(String facePersonFilePath) {
        this.facePersonFilePath = facePersonFilePath;
    }

    public String getFaceLuckDrawListFile() {
        return faceLuckDrawListFile;
    }

    public void setFaceLuckDrawListFile(String faceLuckDrawListFile) {
        this.faceLuckDrawListFile = faceLuckDrawListFile;
    }

    public File[] getFaceImageFile() {
        return faceImageFile;
    }

    public void setFaceImageFile(File[] faceImageFile) {
        this.faceImageFile = faceImageFile;
    }

    public File getFacePersonPath() {
        return facePersonPath;
    }

    public void setFacePersonPath(File facePersonPath) {
        this.facePersonPath = facePersonPath;
    }

    public File getFaceLuckDrawList() {
        return faceLuckDrawList;
    }

    public void setFaceLuckDrawList(File faceLuckDrawList) {
        this.faceLuckDrawList = faceLuckDrawList;
    }
}
